package com.lw.extraceim.net.callback;

import android.util.Log;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.util.JsonUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONArray;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

public abstract class HttpResponseParamCallback extends Callback<HttpResponseParam> {
    private static final String TAG = "HttpResponseParamCallba";

    @Override
    public HttpResponseParam parseNetworkResponse(Response response, int id) throws Exception {
        HttpResponseParam httpResponseParam = new HttpResponseParam();
        String resposneStr = response.body().string();
        Log.d(TAG, "parseNetworkResponse: " + resposneStr);
        if (resposneStr != null) {
            JSONObject jsonObject = new JSONObject(resposneStr);
            String codeJson = jsonObject.getString("code");
            ResponseCode code = JsonUtils.fromJson(codeJson, new TypeToken<ResponseCode>() {
            });
            httpResponseParam.code = code;
            httpResponseParam.reseponseJson = jsonObject;
        } else {
            ResponseCode code = new ResponseCode(ResponseCode.ERROR, "数据内容为空");
        }
        return httpResponseParam;
    }


}
